package engine.gameObject;


import engine.material.DefaultMaterial;
import engine.material.Material;
import engine.shader.Shader;
import engine.util.math.Matrix;

public class RenderObject extends GameObject {

	private Shader shader;
	private Material material = new DefaultMaterial(); 
	
	@Override
	public void render(Shader shader) {
		//1 - enable shader
		if(this.shader != null) {
			this.shader.enable();
			
			//apply lights and material
			getScene().getLightManager().apply(this.shader);
			material.apply(this.shader);
			
			//pass matrices to the shader
			this.shader.sendUniformMatrix4("modelMatrix", getModelMatrix().toFloatBuffer());
			this.shader.sendUniformMatrix4("viewMatrix", getScene().getCurrentCamera().getViewMatrix().toFloatBuffer());
			this.shader.sendUniformMatrix4("projectionMatrix", getScene().getCurrentCamera().getCurrentMatrix().toFloatBuffer());
			this.shader.sendUniformMatrix3("normalMatrix", Matrix.getNormalMatrix(getScene().getCurrentCamera().getViewMatrix(), getModelMatrix()));
			
			//render the object
			renderImpl(this.shader);
			
			shader.enable();
		} else {
			material.apply(shader);
			
			//pass matrices to the shader
			shader.sendUniformMatrix4("modelMatrix", getModelMatrix().toFloatBuffer());
			shader.sendUniformMatrix4("projectionMatrix", getScene().getCurrentCamera().getCurrentMatrix().toFloatBuffer());
			shader.sendUniformMatrix3("normalMatrix", Matrix.getNormalMatrix(getScene().getCurrentCamera().getViewMatrix(), getModelMatrix()));
			
			//render the object
			renderImpl(shader);
		}
	}
	
	public void renderImpl(Shader shader) {}

	public Shader getShader() {
		return shader;
	}

	public void setShader(Shader shader) {
		this.shader = shader;
	}

	public Material getMaterial() {
		return material;
	}

	public void setMaterial(Material material) {
		this.material = material;
	}
}
